//
//  CLComentDataModel.m
//  chanlin
//
//  Created by Ryan on 14-11-22.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "CLComentDataModel.h"
#import "ChangyanSDK.h"
#import "SgrGCD.h"

@implementation CLComentDataModel

SGR_DEF_SINGLETION(CLComentDataModel)


- (void)submitCommentWithTopicId:(NSString *)topicId
                         content:(NSString *)content
                         replyID:(NSString *)replyId
                           block:(void (^)(BOOL success,NSString *commentId))block{
    if([[CLGloble sharedInstance] isLogin]){
        [self submitComment:topicId
                    content:content
                    replyID:replyId
                      block:block];
    }else{
        [self anonymousSubmitCommentWithTopicId:topicId
                                        content:content
                                        replyID:replyId
                                          block:block];
    }
}

- (void)loadTopicWithUrl:(NSString *)url
             title:(NSString *)title
               doc:(NSString *)docId
                   block:(void (^)(BOOL success,NSDictionary *dic))block
{
    __weak typeof(self) me=self;
    [ChangyanSDK loadTopic:_isStrNULL(url)?@"":url
                topicTitle:title
             topicSourceID:docId
                  pageSize:@"20"
                   hotSize:@"5"
             completeBlock:^(CYStatusCode statusCode, NSString *responseStr) {
                 if(statusCode==CYSuccess){
                     [[SgrGCD sharedInstance] enqueueGloble:^{
                         NSDictionary *dic=(NSDictionary *)[[CLGloble sharedInstance] jsonFromString:responseStr];
                         if([dic isKindOfClass:[NSDictionary class]]){
                             NSMutableDictionary *json=[me adoptComment:dic];
                             sgrSafeMainThread(^{
                                 block(YES,json);
                             })
                         }else{
                             sgrSafeMainThread(^{
                                 block(NO,nil);
                             })
                         }
                         
                     }];
                 }else{
                     sgrSafeMainThread(^{
                     block(NO,nil);
                         })
                 }
             }];
}
- (void)loadTopicWithTitle:(NSString *)title
                     topicId:(NSString *)docId
                 pageNum:(int)pageNum
                   block:(void (^)(BOOL success,NSDictionary *dic))block{
    __weak typeof(self) me=self;
    [ChangyanSDK getTopicComments:docId
                         pageSize:@"20"
                           pageNo:[NSString stringWithFormat:@"%d",pageNum]
                    completeBlock:^(CYStatusCode statusCode, NSString *responseStr) {
                        if(statusCode==CYSuccess){
                            [[SgrGCD sharedInstance] enqueueGloble:^{
                                NSDictionary *dic=(NSDictionary *)[[CLGloble sharedInstance] jsonFromString:responseStr];
                                if([dic isKindOfClass:[NSDictionary class]]){
                                    NSMutableDictionary *json=[me adoptComment:dic];
                                    sgrSafeMainThread(^{
                                        block(YES,json);
                                    })
                                }else{
                                    sgrSafeMainThread(^{
                                        block(NO,nil);
                                    })
                                }
                                
                            }];
                        }else{
                            block(NO,nil);
                        }
                    }];
}


- (NSMutableDictionary *)adoptComment:(NSDictionary *)dic{
    NSMutableDictionary *json=[NSMutableDictionary dictionaryWithCapacity:4];
    [json sgrSetObject:[dic sgrFGetStringForKey:@"topic_id"] forKey:@"topic_id"];
    [json sgrSetObject:[dic sgrFGetStringForKey:@"cmt_sum"] forKey:@"count"];
    [json sgrSetObject:[dic sgrFGetStringForKey:@"participation_sum"] forKey:@"join_count"];
    NSMutableDictionary  *comments=[NSMutableDictionary dictionaryWithCapacity:2];
    [json sgrSetObject:comments forKey:@"comments"];
    NSMutableArray *hottest=[NSMutableArray array];
    [comments sgrSetObject:hottest forKey:@"hottest"];
    NSArray *hots=[dic sgrGetArrayForKey:@"hots"];
    for(NSDictionary *hot in hots){
        NSMutableDictionary *comment=[NSMutableDictionary dictionary];
        [comment sgrSetObject:[hot sgrFGetStringForKey:@"comment_id"] forKey:@"comment_id"];
        NSDictionary *passport=[hot sgrGetDictionaryForKey:@"passport"];
        [comment sgrSetObject:[passport sgrGetStringForKey:@"nickname"] forKey:@"uname"];
        [comment sgrSetObject:[hot sgrGetStringForKey:@"content"] forKey:@"comment_contents"];
        [comment sgrSetObject:[hot sgrGetStringForKey:@"ip_location"] forKey:@"ip_from"];
        [comment sgrSetObject:[hot sgrGetStringForKey:@"support_count"] forKey:@"uptimes"];
        [comment sgrSetObject:[passport sgrGetStringForKey:@"img_url"] forKey:@"userFace"];
        [comment sgrSetObject:[passport sgrGetStringForKey:@"profile_url"] forKey:@"profile"];
        [hots sgrAddObject:comment];
    }
    NSMutableArray *newest=[NSMutableArray array];
    [comments sgrSetObject:newest forKey:@"newest"];
    NSArray *cs=[dic sgrGetArrayForKey:@"comments"];
    for(NSDictionary *new in cs){
        NSMutableDictionary *comment=[NSMutableDictionary dictionary];
        NSArray *parents=[new sgrGetArrayForKey:@"comments"];
        NSMutableArray *parents_new=[NSMutableArray array];
        [comment sgrSetObject:parents_new forKey:@"parent"];
        for(NSDictionary *parent in parents){
             NSMutableDictionary *sucomment=[NSMutableDictionary dictionary];
            [sucomment sgrSetObject:[parent sgrFGetStringForKey:@"comment_id"] forKey:@"comment_id"];
            NSDictionary *passport=[parent sgrGetDictionaryForKey:@"passport"];
            [sucomment sgrSetObject:[passport sgrGetStringForKey:@"nickname"] forKey:@"uname"];
            [sucomment sgrSetObject:[parent sgrGetStringForKey:@"content"] forKey:@"comment_contents"];
            [sucomment sgrSetObject:[parent sgrGetStringForKey:@"ip_location"] forKey:@"ip_from"];
            [sucomment sgrSetObject:[parent sgrFGetStringForKey:@"support_count"] forKey:@"uptimes"];
            [sucomment sgrSetObject:[passport sgrGetStringForKey:@"img_url"] forKey:@"userFace"];
            [parents_new sgrAddObject:sucomment];
            
            
        }
        [comment sgrSetObject:[new sgrFGetStringForKey:@"comment_id"] forKey:@"comment_id"];
        NSDictionary *passport=[new sgrGetDictionaryForKey:@"passport"];
        [comment sgrSetObject:[passport sgrGetStringForKey:@"nickname"] forKey:@"uname"];
        [comment sgrSetObject:[new sgrGetStringForKey:@"content"] forKey:@"comment_contents"];
        [comment sgrSetObject:[new sgrGetStringForKey:@"ip_location"] forKey:@"ip_from"];
        [comment sgrSetObject:[new sgrFGetStringForKey:@"support_count"] forKey:@"uptimes"];
        [comment sgrSetObject:[passport sgrGetStringForKey:@"img_url"] forKey:@"userFace"];
        [comment sgrSetObject:[passport sgrGetStringForKey:@"profile_url"] forKey:@"profile"];
        [newest sgrAddObject:comment];
        
    }
    return json;
}

- (void)submitComment:(NSString *)topicID
              content:(NSString *)content
              replyID:(NSString *)replyID
                block:(void (^)(BOOL success,NSString *commentId))block{
    if(_isStrNULL(topicID) || _isStrNULL(content)) return;
    [ChangyanSDK submitComment:topicID
                       content:content
                       replyID:replyID
                         score:nil
                       appType:40
                       picUrls:nil
                 completeBlock:^(CYStatusCode statusCode, NSString *responseStr) {
                     sgrSafeMainThread(^{
                         block(statusCode==CYSuccess,[responseStr sgrFGetStringForKey:@"id"]);
                     });
                 }];
    
}

/*
+ (void)submitComment:(NSString *)topicID
              content:(NSString *)content
              replyID:(NSString *)replyID
                score:(NSString *)score
              appType:(NSInteger )appType
              picUrls:(NSArray *)picUrls
        completeBlock:(CompleteBlock)completeBlock;
 */

- (void)anonymousSubmitCommentWithTopicId:(NSString *)topicId
                                  content:(NSString *)content
                                  replyID:(NSString *)replyId
                                    block:(void (^)(BOOL success,NSString *commentId))block
{
    if(_isStrNULL(topicId) || _isStrNULL(content)) return;
    [ChangyanSDK anonymousSubmitComment:topicId
                                content:content
                                replyID:replyId
                                  score:nil
                                appType:40
                                picUrls:nil
                          completeBlock:^(CYStatusCode statusCode, NSString *responseStr) {
                              sgrSafeMainThread(^{
                                  block(statusCode==CYSuccess,[responseStr sgrFGetStringForKey:@"id"]);
                              });
                          }];
}

- (void)support:(NSString *)topicId
      commentID:(NSString *)commentID
          block:(void (^)(BOOL success,NSString *responseStr))block{
    if(_isStrNULL(topicId) || _isStrNULL(commentID)) return;
    [ChangyanSDK commentAction:1
                       topicID:topicId
                     commentID:commentID
                 completeBlock:^(CYStatusCode statusCode, NSString *responseStr) {
                     sgrSafeMainThread(^{
                         block(statusCode==CYSuccess,responseStr);
                     });
                 }];
}

@end
